package com.akk.business;

import com.akk.common.enums.UserEnums;
import com.akk.common.enums.UserWalletRecordEnums;
import com.akk.exception.RestBusinessException;
import com.akk.module.user.entity.User;
import com.akk.module.user.entity.UserWallet;
import com.akk.module.user.entity.UserWalletRecord;
import com.akk.module.user.model.request.UserDrawCashRequest;
import com.akk.module.user.service.UserCardService;
import com.akk.module.user.service.UserService;
import com.akk.module.user.model.request.UserTransferRequest;
import com.akk.module.user.service.UserWalletRecordService;
import com.akk.module.user.service.UserWalletService;
import com.akk.utils.Arith;
import com.akk.utils.IDUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;


@Service
public class UserWalletBusiness {

    @Autowired
    private UserWalletService userWalletService;
    @Autowired
    private UserWalletRecordService userWalletRecordService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserCardService userCardService;
    //@Autowired
    //private UserWalletRecordService userWalletRecordService;



    public UserWallet walletInfo(User user) {
        UserWallet wallet = userWalletService.findByUserId(user.getUserId());
        Assert.notNull(wallet,"系统异常，请稍后再试.");
        return wallet;
    }


    public boolean transferByPhone(UserTransferRequest request, User user) {
        if(request.getMoney()<= 0){
            throw new RestBusinessException("转账金额错误.");
        }
        if(user.getRealAuth().intValue() != UserEnums.RealAuth.SUCCESS.getValue()){
            throw new RestBusinessException("账户未完成实名认证.");
        }
        User toUser = userService.findByPhoneAndAppId(request.getToPhone(),user.getAppId());
        if(toUser == null){
            throw new RestBusinessException("用户信息不存在");
        }
        if(toUser.getRealAuth().intValue() != UserEnums.RealAuth.SUCCESS.getValue())
            throw new RestBusinessException("对方账户未完成实名认证.");

        UserWallet fromWallet = userWalletService.findByUserId(user.getUserId());//转账发起者
        UserWallet toWallet = userWalletService.findByUserId(toUser.getUserId());//转账接受者
        if(user.getUserId().equals(toUser.getUserId())){
            throw new RestBusinessException("不能向该账户转账.");
        }
        if (!new Sha256Hash(request.getTransPwd()).toHex().equals(user.getTransPwd())){
            throw new RestBusinessException("交易密码错误.");
        }
        if (fromWallet == null || toWallet == null){
            throw new RestBusinessException("转账错误.");
        }
        if (fromWallet.getStatus() == -1){
            throw new RestBusinessException("该账户状态异常.");
        }
        if (toWallet.getStatus() == -1){
            throw new RestBusinessException("对方账户异常.");
        }
        if (fromWallet.getBalance() < request.getMoney()){
            throw new RestBusinessException("账户余额不足.");
        }
        fromWallet.setBalance(Arith.sub(fromWallet.getBalance(),request.getMoney()));
        fromWallet.setUpdateDate(new Date());
        toWallet.setBalance(Arith.add(toWallet.getBalance(),request.getMoney()));
        fromWallet.setUpdateDate(new Date());
        userWalletService.update(fromWallet);
        userWalletService.update(toWallet);
        // 支出记录
        UserWalletRecord fromRecord = new UserWalletRecord();
        fromRecord.setRecordId(IDUtils.getPK());
        fromRecord.setCreateDate(new Date());
        fromRecord.setUserId(fromWallet.getUserId());
        fromRecord.setAmount(request.getMoney());
        fromRecord.setUserId(user.getUserId());
        fromRecord.setRemark("向"+toUser.getUsername()+"转账"+request.getMoney()+"元");
        fromRecord.setToUserId(toWallet.getUserId());
        fromRecord.setRecordStatus(UserWalletRecordEnums.RecordStatus.NORMAL.getValue());
        fromRecord.setRecordType(UserWalletRecordEnums.RecordType.TRANS_OUT.getValue());
        userWalletRecordService.insert(fromRecord);
        // 收款记录
        UserWalletRecord toRecord = new UserWalletRecord();
        toRecord.setFromUserId(fromWallet.getUserId());
        toRecord.setUserId(toUser.getUserId());
        toRecord.setCreateDate(new Date());
        toRecord.setUserId(toWallet.getUserId());
        toRecord.setAmount(request.getMoney());
        toRecord.setRemark("收到"+user.getUsername()+"转账"+request.getMoney()+"元");
        toRecord.setRecordStatus(UserWalletRecordEnums.RecordStatus.NORMAL.getValue());
        toRecord.setRecordType(UserWalletRecordEnums.RecordType.TRANS_IN.getValue());
        userWalletRecordService.insert(toRecord);
        return false;
    }


    public boolean drawCashOrder(UserDrawCashRequest request, User user) {
        return true;
    }
}
